package lumis.service.portalmanagement.group;

import java.util.List;

import lumis.doui.source.AbstractObjectDataProvider;
import lumis.doui.source.TabularSource;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.group.GroupConfig;
import lumis.portal.group.databaseviewmembership.DatabaseViewField;
import lumis.portal.manager.ManagerFactory;
import lumis.util.ITransaction;

/**
 * Populates a source with the {@link DatabaseViewField} relative to a
 * group.
 * <p> 
 * Expects the parameter <em>groupId</em> in the request.
 *
 * @version $Revision: 10458 $ $Date: 2009-06-02 15:49:09 -0300 (Tue, 02 Jun 2009) $
 * @since 4.2.1
 */
public class DatabaseViewFieldByGroupDataProvider extends AbstractObjectDataProvider<TabularSource<?>, DatabaseViewField>
{
	public DatabaseViewFieldByGroupDataProvider()
	{
		super(DatabaseViewField.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected List<DatabaseViewField> loadObjects(SessionConfig sessionConfig, 
			TabularSource<?> source, ITransaction transaction) throws PortalException
	{
		String groupId = source.getDouiContext().getRequest().getParameter("groupId");
		GroupConfig groupConfig = ManagerFactory.getGroupManager().get(sessionConfig, groupId, transaction);
		return ManagerFactory.getEntityManager().createQuery("select f from DatabaseViewField f " +
				"where f.groupTypeConfig.groupType.id = :groupTypeId order by f.columnName")
				.setParameter("groupTypeId", groupConfig.getGroupTypeId())
				.getResultList();
	}
}
